package com.study.example.gameserver.module.player.dao;

import com.sparrow.cache.dao.BaseDaoSupport;
import com.study.example.gameserver.module.player.entity.PlayerEntity;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.stereotype.Component;


@Component
public class PlayerEntityDaoImpl extends BaseDaoSupport<Long, PlayerEntity> implements PlayerEntityDao {

    @Override
    public void deleteAll() {
        Session session = currentSession();
        NativeQuery nativeQuery = session.createNativeQuery("delete from PlayerBaseInfo");
        nativeQuery.executeUpdate();
        session.flush();
    }

    @Override
    public long selectMaxId(int server) {
        Session session = currentSession();
        Long maxId = session.createQuery(
                        "select max(p.playerId) from PlayerEntity p where p.server = :server", Long.class)
                .setParameter("server", server)
                .uniqueResult();
        return maxId != null ? maxId : 0L;
    }
}
